package com.danskin.wechat.web;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import com.github.pagehelper.util.StringUtil;
import com.danskin.wechat.entity.BannerInfo;
import com.danskin.wechat.service.BannerInfoService;
import com.danskin.wechat.util.FTPHelper;
import com.danskin.wechat.util.Page;

@Controller
@RequestMapping("bannerInfo")
public class BannerInfoController extends BaseController{
	@Resource
	private BannerInfoService bannerInfoService;
	/**
	 * 幻灯片管理list
	 * @param bannerInfo
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "queryBannerInfoList", method = RequestMethod.GET)
	public String queryBannerInfoList(BannerInfo bannerInfo,Model model){
		return "bannerInfo/bannerInfo_list";
	}
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/getBannerInfoList", produces = "application/json;charset=UTF-8")
	@ResponseBody
	public Object getBannerInfoList(Page page,HttpServletRequest request) throws Exception {
		Map map = getRequestParameter(this.getRequest());
		map.put("mpId", this.getMpId());
		page.setMap(map);
		List<BannerInfo> listBannerInfo = bannerInfoService.findBannerInfoList(page);
		return pagelistToJSONForBootStrapTable(listBannerInfo, page);
	}
	/**
	 * 新增幻灯片
	 * @return
	 */
	@RequestMapping(value = "openBannerInfo",method = RequestMethod.GET)
	public String openBannerInfo(){
		return "bannerInfo/addBannerInfo";
	}
	/****
	 * 保存新增幻灯片
	 * @param request
	 * @param bannerInfo
	 * @return
	 * @throws IOException 
	 * @throws IllegalStateException 
	 */
	@RequestMapping(value = "saveBannerInfo",method = RequestMethod.POST)
	public String saveBannerInfo(HttpServletRequest request,BannerInfo bannerInfo,@RequestParam("picture")MultipartFile file) throws IllegalStateException, IOException{
		bannerInfo.setMpId(this.getLoginUser().getMpId());
		String id = String.valueOf(bannerInfo.getId());
		String pictureUrl = null;
		try {
			if(!file.isEmpty()){
				 FTPHelper.getInstance();
				 pictureUrl = FTPHelper.getInstance().uploadPictureAndReturnPictureUrl(file, "bannerInfo",
						 FTPHelper.uploadTemporaryFile());
			}					
			bannerInfo.setPicUrl(pictureUrl);
			if("null".equals(id)){
				int cout = bannerInfoService.insertBannerInfo(bannerInfo);
				if(cout>0){
					//System.out.println("保存成功!");
				}
			}else{
				//修改
				bannerInfoService.updateBannerInfo(bannerInfo);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "redirect:/bannerInfo/queryBannerInfoList";
	}
	/**
	 * 删除幻灯片
	 * @param request
	 * @param response
	 */
	@RequestMapping(value = "deleteBannerInfo", method = RequestMethod.POST)
	public void deleteBannerInfo(HttpServletRequest request, HttpServletResponse response){
		boolean flag = false;
		String  id = request.getParameter("id");
		if(StringUtil.isNotEmpty(id)){
			bannerInfoService.deleteBannerInfo(id);
			flag = true;
		}
	    renderData(response, flag);
	}
	/**
	 * 修改幻灯片
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "editBannerInfo",method = RequestMethod.GET)
	public String  editBannerInfo(HttpServletRequest request,Model model){
		String id = request.getParameter("id");
		BannerInfo bannerInfo = null;
		if(StringUtil.isNotEmpty(id)){
			bannerInfo = bannerInfoService.findBannerInfobyId(id);
		}
		model.addAttribute("bannerInfo", bannerInfo);
		return "bannerInfo/updateBannerInfo";
	}
	private void renderData(HttpServletResponse response, boolean data) {
		PrintWriter printWriter = null;
		try {
			printWriter = response.getWriter();
			printWriter.print(data);
		} catch (IOException ex) {
			ex.printStackTrace();
		} finally {
			if (null != printWriter) {
				printWriter.flush();
				printWriter.close();
			}
		}
	}
}
